// function reverse(arr) {
//   const length = arr.length
//   let temp = null
//   for(let i = 0; i< length/2; i++){
//     temp = arr[i]
//     arr[i] = arr[length - 1 - i]
//     arr[length -1-i] = temp
//   }
//   return arr
// }

// console.time(222)
// console.log(reverse([1,2,3,4,5]))
// console.timeEnd(222)



function ListNode(val, next) {
  this.val = (val===undefined ? 0 : val)
  this.next = (next===undefined ? null : next)
}
const h5 = new ListNode(5, null)
const h4 = new ListNode(4, h5)
const h3 = new ListNode(3, h4)
const h2 = new ListNode(2, h3)
const h1 = new ListNode(1, h2)

// function reverseList(head) {
//   let cur = head
//   let pre = null  
//   while(cur) {
//     let next = cur.next
//     cur.next = pre
//     pre = cur
//     cur = next
//   }
// }
// function reverseList(head) {
//   let pre = head
//   let cur = head.next
//   let next = cur.next
//   while ( cur ) {
//     cur.next = pre
//     pre = cur
//     cur = next
//     next = cur && cur.next    
//   }
//   return cur || pre
// }

// function reverseList(head) {
//   let cur = head
//   let pre = null
//   while(cur) {
//     let next = cur.next
//     cur.next = pre
//     pre = cur
//     cur = next
//   }
//   return pre
// }

// console.log(reverseList(h1))


// function myReverse(arr) {
//   let length = arr.length
//   for(let i = 0; i< length/2; i++){
//     const temp = arr[i]
//     arr[i] = arr[length-1-i]
//     arr[length-1-i] = temp
//   }
//   return arr
// }
// console.log(myReverse([1,2,3,4,5,6,7,8,9]))


function myReverse(arr) {
  const len = arr.length
  for( let i = 0; i < len/2; i++) {
    console.log(i)
    const temp = arr[i]
    arr[i] = arr[len-1-i]
    arr[len-1-i] = temp
  }
  return arr
}

console.log(myReverse([1,2,3,4,5,6,7,8,9]))